package com.example.boot;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
//import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.boot.order.model.AuthorSettings;
import com.example.boot.socket.config.SocketMessage;

@Controller
@ComponentScan
@MapperScan("com.example.boot.*.dao")
@SpringBootApplication
//@WebAppConfiguration
public class BootApplication    
extends SpringBootServletInitializer { 
//	implements  EmbeddedServletContainerCustomizer {
	@Autowired
	private AuthorSettings authorSettings;
	@Autowired
	private SimpMessagingTemplate messagingTemplate;
//	@Autowired
//	private RedisSocketService redisSocketService;


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BootApplication.class);
    }
	
	public static void main(String[] args) {
		SpringApplication.run(BootApplication.class, args);
	}

	@RequestMapping(value = "/socket", method = RequestMethod.GET)
	// @ResponseBody
	public String aa(String name, Model modelAndView) {
		modelAndView.addAttribute("name", name);
		// Order o = new Order();
		// o.setId("1231");

		return "index";
	}

	@RequestMapping(value = "/loginpage", method = RequestMethod.GET)
	public String login() {

		return "demo";
	}

	@MessageMapping("/send")
	@SendTo("/topic/send")
	public SocketMessage send(SocketMessage message) throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		redisSocketService.saveRecord(message.getName(), message.getMessage());
		message.date = df.format(new Date());

		return message;
	}

	@Scheduled(fixedRate = 1000)
	@SendTo("/topic/callback")
	public Object callback() throws Exception {
		// 发现消息
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		messagingTemplate.convertAndSend("/topic/callback", df.format(new Date()));
		messagingTemplate.convertAndSend("/topic/callback", "袁德旭");
		return "callback";
	}

//	@Override
//	public void customize(ConfigurableEmbeddedServletContainer arg0) {
//		arg0.setPort(8080);
//	}



}
